package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.utils.RDateUtils;
import com.rockcent.mall.lib.domain.enumclass.MarketMerchantCooperationStatus;
import com.rockcent.mall.lib.domain.enumclass.MerchantCooperationType;
import com.rockcent.mall.lib.dto.MarketMerchantCooperationDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by oyhk on 14-6-22.
 */
@Repository
public class MarketMerchantCooperationRepositoryImpl implements MarketMerchantCooperationRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(MarketMerchantCooperationRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<MarketMerchantCooperationDto> findPage(Long merchantId, MarketMerchantCooperationDto marketMerchantCooperationDto, PageResult pageResult) {
        SQLPageDoing<MarketMerchantCooperationDto> doing = (sql, fields, params, typeReferenceMap) -> {

            if (MerchantCooperationType.DISTRIBUTOR == MerchantCooperationType.valueOf(marketMerchantCooperationDto.getType())) {
                sql.append("select %s from market_merchant_cooperation mc left join merchant m on m.id = mc.distributor_merchant_id where mc.supplier_merchant_id = :merchantId  and mc.status= :status");
                fields.append("mc.*,m.name distributor_merchant_name");
            } else if (MerchantCooperationType.SUPPLIER == MerchantCooperationType.valueOf(marketMerchantCooperationDto.getType())) {
                sql.append("select %s from market_merchant_cooperation mc left join merchant m on m.id = mc.supplier_merchant_id where mc.distributor_merchant_id = :merchantId and mc.status= :status");
                fields.append("mc.*,m.name supplier_merchant_name");
            }

            // 名称搜索
            if (StringUtils.isNotBlank(marketMerchantCooperationDto.getSearchMerchantName())) {
                sql.append(" and m.name like :merchantName");
                params.put("merchantName", "%" + marketMerchantCooperationDto.getSearchMerchantName() + "%");
            }
            // 时间搜索
            if (StringUtils.isNotBlank(marketMerchantCooperationDto.getSearchStartTime()) && StringUtils.isBlank(marketMerchantCooperationDto.getSearchEndTime())) {
                sql.append(" and mc.date_created > :searchStartTime");
                params.put("searchStartTime", RDateUtils.parseDate(marketMerchantCooperationDto.getSearchStartTime(), RDateUtils.parsePattern));
            } else if (StringUtils.isNotBlank(marketMerchantCooperationDto.getSearchStartTime()) && StringUtils.isNotBlank(marketMerchantCooperationDto.getSearchEndTime())) {
                sql.append(" and mc.date_created > :searchStartTime and mc.date_created < :searchEndTime");
                params.put("searchStartTime", RDateUtils.parseDate(marketMerchantCooperationDto.getSearchStartTime(), RDateUtils.parsePattern));
                params.put("searchEndTime", RDateUtils.parseDate(marketMerchantCooperationDto.getSearchEndTime(), RDateUtils.parsePattern));
            } else if (StringUtils.isBlank(marketMerchantCooperationDto.getSearchStartTime()) && StringUtils.isNotBlank(marketMerchantCooperationDto.getSearchEndTime())) {
                sql.append(" and mc.date_created < :searchEndTime");
                params.put("searchEndTime", RDateUtils.parseDate(marketMerchantCooperationDto.getSearchEndTime(), RDateUtils.parsePattern));
            }

            sql.append(" order by mc.date_created desc ");

            params.put("merchantId", merchantId);
            params.put("status", MarketMerchantCooperationStatus.PASS.toString());

        };
        return doing.go(pageResult, MarketMerchantCooperationDto.class, objectMapper, jdbcTemplate, log);
    }

    /**
     *
     * @param mallId
     * @param merchantId
     * @return
     */
    @Override
    public Long getMarketMerchantWaitVerify(Long mallId, Long merchantId) {
        SQLObjectDoing<Long> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM market_merchant_cooperation_record mmc  WHERE mmc.`status`='WAIT' " +
                    "  AND  mmc.`supplier_merchant_id`=:merchantId" +
                    "  AND mmc.`supplier_mall_id`=:mallId" +
                    "  AND mmc.is_delete='NO'");
            params.put("merchantId",merchantId);
            params.put("mallId",mallId);
            fields.append("COUNT(mmc.`id`)");
        };
        return doing.go(Long.class, jdbcTemplate, log,objectMapper);
    }
}
